package com.epta.service;

import com.epta.mapper.RequirementMapper;
import com.epta.pojo.Requirement;
import com.epta.common.util.DateTimeUtil;
import com.epta.vo.RequirementBackListVo;
import com.epta.vo.RequirementFrontDetailVo;
import com.epta.vo.RequirementFrontListVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19 0019.
 */
@Service
public class RequirementService extends BaseService<Requirement> {
    @Autowired
    private RequirementMapper requirementMapper;
    @Autowired
    private UserService userService;

    public PageInfo<RequirementBackListVo> queryBackList(int page, int rows){
        PageInfo pageInfo = this.queryPageListByWhere(null,page,rows);
        List<RequirementBackListVo> list = new ArrayList<>();
        for(Object o : pageInfo.getList()){
            Requirement requirement = (Requirement)o;
            RequirementBackListVo requirementBackListVo = assembleRequirementBackListVo(requirement);
            list.add(requirementBackListVo);
        }
        pageInfo.setList(list);
        return pageInfo;
    }

    public PageInfo<RequirementFrontListVo> queryFrontList(int page, int rows){
        PageInfo pageInfo = this.queryPageListByWhere(null,page,rows);
        List<RequirementFrontListVo> list = new ArrayList<>();
        for(Object o : pageInfo.getList()){
            Requirement requirement = (Requirement)o;
            RequirementFrontListVo requirementFrontListVo = assembleRequirementFrontListVo(requirement);
            list.add(requirementFrontListVo);
        }
        pageInfo.setList(list);
        return pageInfo;
    }

    public RequirementFrontDetailVo queryFrontDetail(int id){
//        Requirement requirement = this.queryById(id);
//        Requirement next = requirementMapper.selectNext(requirement.getId(),requirement.getSortOrder());
//        Requirement pre = requirementMapper.selectPre(requirement.getId(),requirement.getSortOrder());
//        RequirementFrontDetailVo requirementFrontDetailVo = assembleRequirementFrontDetailVo(requirement,next,pre);
//        return requirementFrontDetailVo;
        return new RequirementFrontDetailVo();
    }

    private RequirementFrontDetailVo assembleRequirementFrontDetailVo(Requirement requirement, Requirement next, Requirement pre){
        RequirementFrontDetailVo requirementFrontDetailVo = new RequirementFrontDetailVo();
        requirementFrontDetailVo.setId(requirement.getId());
        requirementFrontDetailVo.setTime(DateTimeUtil.dateToStr(requirement.getCreated(),"yyyy-MM-dd"));
        requirementFrontDetailVo.setTitle(requirement.getTitle());
        requirementFrontDetailVo.setContent(requirement.getContent());
        requirementFrontDetailVo.setUserName(userService.queryById(requirement.getUserId()).getName());
        if(next != null){
            requirementFrontDetailVo.setNextId(next.getId());
            requirementFrontDetailVo.setNextTitle(next.getTitle());
        }
        if(pre != null){
            requirementFrontDetailVo.setPreId(pre.getId());
            requirementFrontDetailVo.setPreTitle(pre.getTitle());
        }
        return requirementFrontDetailVo;
    }

    private RequirementFrontListVo assembleRequirementFrontListVo(Requirement requirement){
        RequirementFrontListVo requirementFrontListVo = new RequirementFrontListVo();
        requirementFrontListVo.setId(requirement.getId());
        requirementFrontListVo.setTitle(requirement.getTitle());
        requirementFrontListVo.setTime(DateTimeUtil.dateToStr(requirement.getCreated(),"yyyy-MM-dd"));
        return requirementFrontListVo;
    }
    private RequirementBackListVo assembleRequirementBackListVo(Requirement requirement){
        RequirementBackListVo requirementBackListVo = new RequirementBackListVo();
        requirementBackListVo.setId(requirement.getId());
        requirementBackListVo.setTitle(requirement.getTitle());
        requirementBackListVo.setTime(DateTimeUtil.dateToStr(requirement.getCreated()));
        requirementBackListVo.setUserName(userService.queryById(requirement.getUserId()).getName());
        return requirementBackListVo;
    }
}
